﻿<script language="jscript" runat="server">
function ActionJscript(){
	this.empty=function(a){
		F.echo("没找到动作：" + a);
	};
}
ActionJscript.prototype=new IAction(); //继承IAction对象，下面才可以直接用this.assign，this.display
ActionJscript.prototype.Index=function(){
	Model__("Public").orderby("id asc").limit(F.get.int("page",1),5).query().assign("List");
	var id = F.get.int("id",0);
	if(id>0)
		Model__("Public").where("id=" + id).query().assign("Item",true);
	else{
		//如果id不大于0，构造一条记录，用来新增，Record__的assign方法无其他构造
		var item = Record__();
		item.set("name","");
		item.set("age","0");
		item.set("birthday",F.formatdate(new Date(),"yyyy-MM-dd"));
		item.set("memo","");
		item.set("grade","0");
		item.assign("Item");
	}
	//将id赋值为全局变量
	this.assign("id",id);
	//将数据显示到模板，因为这里用的是Action.Data，所以需要用Home:来引用Actin.Home的模板
	this.display("Home:Data");
};
	
ActionJscript.prototype.com=function(){
	Model__("Public A","id").select("A.*,B.name as gname").join("Grade B").on("A.Grade=B.id").where("A.id>=3").orderby("A.id asc").limit(F.get.int("page",1),5).query().assign("List");
	this.display("Home:Data2");
};
ActionJscript.prototype.all=function(){
	this.assign("recordcount",Model__("Public","id").orderby("id asc").query().assign("List").fetch().recordcount);
	this.display("Home:Data3");
};
ActionJscript.prototype.modify=function(){
	F.post("name",F.post.safe("name"));
	F.post("memo",F.post.safe("memo"));
	var FV = Mo("FormValidatee");
	FV.AddRule("id","required;numeric","ID错误");
	FV.AddRule("name","required;min-length:5;max-length:20");
	FV.AddRule("age","required;between:1,200");
	FV.AddRule("birthday","required;exp:/^(\\d{4})\\-(\\d{1,2})\\-(\\d{1,2})$/");
	FV.AddRule("memo","max-length:50");
	FV.AddRule("grade","required;numeric","级别必须为数字");
	if(FV.Validate()){
		if(F.post.int("id",0)==0){
			//直接从post读数据，插入数据库，如果担心安全问题，请使用Record__构造一个字典，作为insert的参数传递
			Model__("Public","id").insert();
		}else{
			//直接从post读数据，更新数据库，如果担心安全问题，请使用Record__构造一个字典，作为update的参数传递
			Model__("Public","id").update();
		}
		F.goto("?m=" + MO_METHOD,"保存成功");
	}else{
		F.goto("?m=" + MO_METHOD,FV.exception);
	}
};
//因为delete是jscript关键字，因此没用.的方式定义
ActionJscript.prototype["delete"]=function(){
	//删除记录
	Model__("Public","id").where("id=" + F.get.int("id")).Delete();
	F.goto("?m=" + MO_METHOD,"删除完成");
};
</script>